package com.spittr.web.repository;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
@SuppressWarnings("unchecked")
public class HibernateRepositoryImpl<T> implements HibernateRepository<T> {
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Session currentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	@Transactional(readOnly=true)
	public T queryEntityById(Class<T> clazz,String id) {
		return (T) currentSession().get(clazz, id);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void saveOrUpdateEntity(T t) {
		currentSession().saveOrUpdate(t);
	}

}
